home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / initramfs-tools / scripts / functions next >
Encoding:
Text File  |  2012-06-06  |  8.1 KB  |  426 lines

  1. # -*- shell-script -*-
  2.  
  3. _log_msg()
  4. {
  5.     if [ "$quiet" = "y" ]; then return; fi
  6.     printf "$@"
  7. }
  8.  
  9. log_success_msg()
  10. {
  11.     _log_msg "Success: $@\n"
  12. }
  13.  
  14. log_failure_msg()
  15. {
  16.     _log_msg "Failure: $@\n"
  17. }
  18.  
  19. log_warning_msg()
  20. {
  21.     _log_msg "Warning: $@\n"
  22. }
  23.  
  24. log_begin_msg()
  25. {
  26.     _log_msg "Begin: $@ ... "
  27. }
  28.  
  29. log_end_msg()
  30. {
  31.     _log_msg "done.\n"
  32. }
  33.  
  34. panic()
  35. {
  36.     if command -v chvt >/dev/null 2>&1; then
  37.         chvt 1
  38.     fi
  39.  
  40.     echo "$@"
  41.     # Disallow console access
  42.     if [ -n "${panic}" ]; then
  43.         echo "Rebooting automatically due to panic= boot argument"
  44.         sleep ${panic}
  45.         reboot
  46.     fi
  47.     modprobe -v i8042 || true
  48.     modprobe -v atkbd || true
  49.     modprobe -v ehci-hcd || true
  50.     modprobe -v uhci-hcd || true
  51.     modprobe -v ohci-hcd || true
  52.     modprobe -v usbhid || true
  53.     REASON="$@" PS1='(initramfs) ' /bin/sh -i </dev/console >/dev/console 2>&1
  54. }
  55.  
  56. maybe_break()
  57. {
  58.     if [ "${break:-}" = "$1" ]; then
  59.         panic "Spawning shell within the initramfs"
  60.     fi
  61. }
  62.  
  63. render()
  64. {
  65.     eval "echo -n \${$@}"
  66. }
  67.  
  68. set_initlist()
  69. {
  70.     unset initlist
  71.     for si_x in ${initdir}/*; do
  72.         # skip empty dirs without warning
  73.         [ "${si_x}" = "${initdir}/*" ] && return
  74.  
  75.         # only allow variable name chars
  76.         case ${si_x#${initdir}/} in
  77.         *[![:alnum:]\._-]*)
  78.             [ "${verbose}" = "y" ] \
  79.             && echo "$si_x ignored: not alphanumeric or '_' file" >&2
  80.             continue
  81.             ;;
  82.         esac
  83.  
  84.         # skip non executable scripts
  85.         if [ ! -x ${si_x} ]; then
  86.             [ "${verbose}" = "y" ] \
  87.             && echo "$si_x ignored: not executable" >&2
  88.             continue
  89.         fi
  90.  
  91.         # skip directories
  92.         if [ -d ${si_x} ]; then
  93.             [ "${verbose}" = "y" ] \
  94.             && echo "$si_x ignored: a directory" >&2
  95.             continue
  96.         fi
  97.  
  98.         # skip bad syntax
  99.         if ! sh -n ${si_x} ; then
  100.             [ "${verbose}" = "y" ] \
  101.             && echo "$si_x ignored: bad syntax" >&2
  102.             continue
  103.         fi
  104.  
  105.         initlist="${initlist:-} ${si_x#${initdir}/}"
  106.     done
  107. }
  108.  
  109. reduce_satisfied()
  110. {
  111.     deplist="$(render array_${1})"
  112.     unset tmpdeplist
  113.     for rs_y in ${deplist}; do
  114.         # only allow variable name chars
  115.         case ${rs_y} in
  116.         *[![:alnum:]\._-]*)
  117.             continue
  118.             ;;
  119.         esac
  120.         # skip non executable scripts
  121.         [ ! -x ${initdir}/${rs_y} ] && continue
  122.         # skip directories
  123.         [ -d ${initdir}/${rs_y} ] && continue
  124.         # skip bad syntax
  125.         sh -n ${initdir}/${rs_y} || continue
  126.  
  127.         tmpdeplist="${tmpdeplist} ${rs_y}"
  128.     done
  129.     deplist=${tmpdeplist}
  130.     for rs_x in ${runlist}; do
  131.         pop_list_item ${rs_x} ${deplist}
  132.         deplist=${tmppop}
  133.     done
  134.     eval array_${1}=\"${deplist}\"
  135. }
  136.  
  137. get_prereqs()
  138. {
  139.     set_initlist
  140.     for gp_x in ${initlist}; do
  141.         tmp=$(${initdir}/${gp_x} prereqs)
  142.         eval array_${gp_x}=\"${tmp}\"
  143.     done
  144. }
  145.  
  146. count_unsatisfied()
  147. {
  148.     set -- ${@}
  149.     return ${#}
  150. }
  151.  
  152. # Removes $1 from initlist
  153. pop_list_item()
  154. {
  155.     item=${1}
  156.     shift
  157.     set -- ${@}
  158.     unset tmppop
  159.     # Iterate
  160.     for pop in ${@}; do
  161.         if [ ${pop} = ${item} ]; then
  162.             continue
  163.         fi
  164.         tmppop="${tmppop} ${pop}"
  165.     done
  166.  
  167. }
  168.  
  169. # This function generates the runlist, so we clear it first.
  170. reduce_prereqs()
  171. {
  172.     unset runlist
  173.     set -- ${initlist}
  174.     i=$#
  175.     # Loop until there's no more in the queue to loop through
  176.     while [ ${i} -ne 0 ]; do
  177.         oldi=${i}
  178.         for rp_x in ${initlist}; do
  179.             reduce_satisfied ${rp_x}
  180.             count_unsatisfied $(render array_${rp_x})
  181.             cnt=${?}
  182.             if [ ${cnt} -eq 0 ]; then
  183.                 runlist="${runlist} ${rp_x}"
  184.                 pop_list_item ${rp_x} ${initlist}
  185.                 initlist=${tmppop}
  186.                 i=$((${i} - 1))
  187.             fi
  188.         done
  189.         if [ ${i} -eq ${oldi} ]; then
  190.             panic "PANIC: Circular dependancy.  Exiting."
  191.         fi
  192.     done
  193. }
  194.  
  195. get_prereq_pairs()
  196. {
  197.     set_initlist
  198.     for gp_x in ${initlist:-}; do
  199.         echo ${gp_x} ${gp_x}
  200.         prereqs=$(${initdir}/${gp_x} prereqs)
  201.         for prereq in ${prereqs}; do
  202.             echo ${prereq} ${gp_x}
  203.         done
  204.     done
  205. }
  206.  
  207. call_scripts()
  208. {
  209.     set -e
  210.     for cs_x in ${runlist}; do
  211.         [ -f ${initdir}/${cs_x} ] || continue
  212.         # mkinitramfs verbose output
  213.         if [ "${verbose}" = "y" ]; then
  214.             echo "Calling hook ${cs_x}"
  215.         fi
  216.         ${initdir}/${cs_x} && ec=$? || ec=$?
  217.         # allow hooks to abort build:
  218.         if [ "$ec" -ne 0 ]; then
  219.             echo "E: ${initdir}/${cs_x} failed with return $ec."
  220.             # only errexit on mkinitramfs
  221.             [ -n "${version}" ] && exit $ec
  222.         fi
  223.         # allow boot scripts to modify exported boot parameters
  224.         if [ -e /conf/param.conf ]; then
  225.             . /conf/param.conf
  226.         fi
  227.     done
  228.     set +e
  229. }
  230.  
  231. run_scripts()
  232. {
  233.     initdir=${1}
  234.     [ ! -d ${initdir} ] && return
  235.  
  236.     if [ -f ${initdir}/ORDER ]; then
  237.         . ${initdir}/ORDER
  238.     elif command -v tsort >/dev/null 2>&1; then
  239.         runlist=$(get_prereq_pairs | tsort)
  240.         call_scripts ${2:-}
  241.     else
  242.         get_prereqs
  243.         reduce_prereqs
  244.         call_scripts
  245.     fi
  246. }
  247.  
  248. # Load custom modules first
  249. load_modules()
  250. {
  251.     if [ -e /conf/modules ]; then
  252.         cat /conf/modules | while read m; do
  253.             # Skip empty lines
  254.             if [ -z "$m" ];  then
  255.                 continue
  256.             fi
  257.             # Skip comments - d?ash removes whitespace prefix
  258.             com=$(printf "%.1s" "${m}")
  259.             if [ "$com" = "#" ]; then
  260.                 continue
  261.             fi
  262.             modprobe $m
  263.         done
  264.     fi
  265. }
  266.  
  267. # lilo compatibility
  268. parse_numeric() {
  269.     case $1 in
  270.     "")
  271.         return
  272.         ;;
  273.     /*)
  274.         return
  275.         ;;
  276.     [0-9]*:[0-9]*)
  277.         minor=$(( ${1#*:} ))
  278.         major=$(( ${1%:*} ))
  279.         ;;
  280.     [A-Fa-f0-9]*)
  281.         value=$(( 0x${1} ))
  282.         minor=$(( ${value} % 256 ))
  283.         major=$(( ${value} / 256 ))
  284.         ;;
  285.     *)
  286.         return
  287.         ;;
  288.     esac
  289.  
  290.     if command -v udevd >/dev/null 2>&1; then
  291.         ROOT=/dev/block/${major}:${minor}
  292.     else
  293.         mknod -m 600 /dev/root b ${major} ${minor}
  294.         ROOT=/dev/root
  295.     fi
  296. }
  297.  
  298. # Parameter: device node to check
  299. # Echos fstype to stdout
  300. # Return value: indicates if an fs could be recognized
  301. get_fstype ()
  302. {
  303.     local FS FSTYPE FSSIZE RET
  304.     FS="${1}"
  305.  
  306.     # blkid has a more complete list of file systems,
  307.     # but fstype is more robust
  308.     eval $(fstype "${FS}" 2> /dev/null)
  309.     if [ "$FSTYPE" = "unknown" ] &&  command -v blkid >/dev/null 2>&1 ; then
  310.         FSTYPE=$(blkid -o value -s TYPE "${FS}")
  311.     elif [ "$FSTYPE" = "unknown" ] && [ -x /lib/udev/vol_id ]; then
  312.         FSTYPE=$(/lib/udev/vol_id -t "${FS}" 2> /dev/null)
  313.     fi
  314.     RET=$?
  315.  
  316.     if [ -z "${FSTYPE}" ]; then
  317.         FSTYPE="unknown"
  318.     fi
  319.  
  320.     echo "${FSTYPE}"
  321.     return ${RET}
  322. }
  323.  
  324. configure_networking()
  325. {
  326.     if [ -n "${BOOTIF}" ]; then
  327.         # pxelinux sets BOOTIF to a value based on the mac address of the
  328.         # network card used to PXE boot, so use this value for DEVICE rather
  329.         # than a hard-coded device name from initramfs.conf. this facilitates
  330.         # network booting when machines may have multiple network cards.
  331.         # pxelinux sets BOOTIF to 01-$mac_address
  332.  
  333.         # strip off the leading "01-", which isn't part of the mac
  334.         # address
  335.         temp_mac=${BOOTIF#*-}
  336.  
  337.         # convert to typical mac address format by replacing "-" with ":"
  338.         bootif_mac=""
  339.         IFS='-'
  340.         for x in $temp_mac ; do
  341.             if [ -z "$bootif_mac" ]; then
  342.                 bootif_mac="$x"
  343.             else
  344.                 bootif_mac="$bootif_mac:$x"
  345.             fi
  346.         done
  347.         unset IFS
  348.  
  349.         # look for devices with matching mac address, and set DEVICE to
  350.         # appropriate value if match is found.
  351.         for device in /sys/class/net/* ; do
  352.             if [ -f "$device/address" ]; then
  353.                 current_mac=$(cat "$device/address")
  354.                 if [ "$bootif_mac" = "$current_mac" ]; then
  355.                     DEVICE=${device##*/}
  356.                     break
  357.                 fi
  358.             fi
  359.         done
  360.     fi
  361.  
  362.     # networking already configured thus bail out
  363.     [ -n "${DEVICE}" ] && [ -e /run/net-"${DEVICE}".conf ] && return 0
  364.  
  365.     wait_for_udev 10
  366.  
  367.     # support ip options see linux sources
  368.     # Documentation/filesystems/nfs/nfsroot.txt
  369.     # Documentation/frv/booting.txt
  370.  
  371.     for ROUNDTTT in 2 3 4 6 9 16 25 36 64 100; do
  372.  
  373.         # The NIC is to be configured if this file does not exist.
  374.         # Ip-Config tries to create this file and when it succeds
  375.         # creating the file, ipconfig is not run again.
  376.         for x in /run/net-"${DEVICE}".conf /run/net-*.conf ; do
  377.             [ -e "$x" ] && break 2
  378.         done
  379.  
  380.         case ${IP} in
  381.         none|off)
  382.             # Do nothing
  383.             ;;
  384.         ""|on|any)
  385.             # Bring up device
  386.             ipconfig -t ${ROUNDTTT} "${DEVICE}"
  387.             ;;
  388.         dhcp|bootp|rarp|both)
  389.             ipconfig -t ${ROUNDTTT} -c ${IP} -d "${DEVICE}"
  390.             ;;
  391.         *)
  392.             ipconfig -t ${ROUNDTTT} -d $IP
  393.  
  394.             # grab device entry from ip option
  395.             NEW_DEVICE=${IP#*:*:*:*:*:*}
  396.             if [ "${NEW_DEVICE}" != "${IP}" ]; then
  397.                 NEW_DEVICE=${NEW_DEVICE%:*}
  398.             else
  399.                 # wrong parse, possibly only a partial string
  400.                 NEW_DEVICE=
  401.             fi
  402.             if [ -n "${NEW_DEVICE}" ]; then
  403.                 DEVICE="${NEW_DEVICE}"
  404.             fi
  405.             ;;
  406.         esac
  407.     done
  408.  
  409.     # source ipconfig output
  410.     if [ -n "${DEVICE}" ]; then
  411.         # source specific bootdevice
  412.         . /run/net-${DEVICE}.conf
  413.     else
  414.         # source any interface...
  415.         # ipconfig should have quit after first response
  416.         . /run/net-*.conf
  417.     fi
  418. }
  419.  
  420. # Wait for queued kernel/udev events
  421. wait_for_udev()
  422. {
  423.     command -v udevadm >/dev/null 2>&1 || return 0
  424.     udevadm settle ${1:+--timeout=$1}
  425. }
  426.